package dao

import (
	"fmt"
	"log"
	"testhttp/datasource"
	"testhttp/model"
)

func SelectDealer(param model.SelectDealer) ([]model.Dealer, error) {
	offset := (param.Page - 1) * param.Pagesize

	res := make([]model.Dealer, 0)
	// log.Print("Debug :", offset, ",", param.Where)
	err := datasource.InstanceMaster().Where(param.Where, 0).Limit(param.Pagesize, offset).Find(&res)
	if err != nil {
		log.Print("Error SelectDealer():", err)
	}
	// log.Print("Debug dao res:", res)
	return res, err
}

func CountDealer(param model.SelectDealer) (int64, error) {
	uniq := new(model.Dealer)
	count, err := datasource.InstanceMaster().Where(param.Where, 0).Count(uniq)
	if err != nil {
		log.Print("Error ListDealer():", err)
	}
	return count, err
}

func InsertDealer(dealer []model.Dealer) error {
	affected, err := datasource.InstanceMaster().Insert(dealer)
	log.Printf("Success! %d records insert.\n", affected)
	if err != nil {
		log.Print("Error InsertDealer():", err)
	}
	return err
}

func ViewDealer(id string) []model.Dealer {
	res := make([]model.Dealer, 0)
	err := datasource.InstanceMaster().Where("id =?", id).Find(&res)
	if err != nil {
		log.Print("Error ViewDealer():", err)
	}
	return res
}

func UpdateDealer(dealer []model.Dealer) (int, int) {
	var sum_err int
	var success int
	for x := 0; x < len(dealer); x++ {
		// log.Print("Debug dealer:", dealer[x])
		affected, err := datasource.InstanceMaster().Where("dealer_uid=?", dealer[x].DealerUid).Update(dealer[x])

		if err != nil {
			log.Print("Error UpdateDealer():", err)
			sum_err++
		} else {
			log.Printf("%d records updated.\n", affected)
			success++
		}

	}
	return success, sum_err
}

func GetDealer(id string) string {
	res := make([]model.Dealer, 0)
	err := datasource.InstanceMaster().Where("dealer_uid =?", id).Find(&res)
	if err != nil {
		log.Print("Error GetDealer():", err)
	}
	if len(res) == 0 {
		dealer := ""
		return dealer
	} else {
		dealer := res[0].Dealer
		return dealer
	}

}
func ListDealer(dealer, shop string, pagesize, page int) ([]model.Dealer, int64) {

	uniq := new(model.Dealer)
	res := make([]model.Dealer, 0)
	offset := (page - 1) * pagesize

	if dealer == "" && shop == "" {
		count, err1 := datasource.InstanceMaster().Where("is_deleted =?", 0).Count(uniq)
		if err1 != nil {
			log.Print("Error ListDealer():", err1)
		}
		err := datasource.InstanceMaster().Where("is_deleted =?", 0).Limit(pagesize, offset).Find(&res)
		if err != nil {
			log.Print("Error ListDealer():", err)
		}
		return res, count
	} else if dealer == "" {
		count, err1 := datasource.InstanceMaster().Where(fmt.Sprintf(" shop like '%s' and is_deleted = 0", "%"+shop+"%")).Count(uniq)
		if err1 != nil {
			log.Print("Error ListDealer():", err1)
		}
		err := datasource.InstanceMaster().Where(fmt.Sprintf("shop like '%s' and is_deleted = 0", "%"+shop+"%")).Limit(pagesize, offset).Find(&res)
		if err != nil {
			log.Print("Error ListDealer():", err)
		}
		return res, count
	} else {
		count, err1 := datasource.InstanceMaster().Where(fmt.Sprintf("dealer like '%s' and is_deleted = 0", "%"+dealer+"%")).Count(uniq)
		if err1 != nil {
			log.Print("Error ListDealer():", err1)
		}
		err := datasource.InstanceMaster().Where(fmt.Sprintf("dealer like '%s'  and is_deleted = 0", "%"+dealer+"%")).Limit(pagesize, offset).Find(&res)
		if err != nil {
			log.Print("Error ListDealer():", err)
		}
		return res, count
	}
}
